package com.json.webapp.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.json.model.TaskMaster;
import com.opensymphony.xwork2.Preparable;

public class TaskMasterAction extends BaseAction implements Preparable {

    private static final long serialVersionUID = 6776558938712115191L;
    private List<TaskMaster> taskMasterList;
    private TaskMaster task;
    private String id;
    private String query;

    
	
	
	
	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		
	     if (getRequest().getMethod().equalsIgnoreCase("post") && (!"".equals(getRequest().getParameter("task.id")))) {
	            task = taskManager.get(new Long(getRequest().getParameter("task.id")));
	      }
	   
	}
	
	 public String delete() {
		 taskManager.remove(task.getId());
	     List<Object> args = new ArrayList<Object>();
	     args.add(task.getTaskDescrp());
	     saveMessage(getText("task.deleted", args));
	     return SUCCESS;
	 }
	
	
	  public String edit() throws IOException {
		  if (id!=null){
			  task = taskManager.get(new Long(id));			  
		  }else{
			  task = new TaskMaster();
		  }
		  return INPUT;
			  
	  }
	  
	public String list() {
		taskMasterList = taskManager.getAll();
		return SUCCESS;
	}

	public String save() throws Exception {
	    if (cancel != null) {
	        return CANCEL;
	    }
	 
	    if (delete != null) {
	        return delete();
	    }
	 
	    boolean isNew = (task.getId() == null);
	 
	    task = taskManager.save(task);
	 
	    String key = (isNew) ? "task.added" : "task.updated";
	    saveMessage(getText(key));
	 
	    if (!isNew) {
	        return "home";
	    } else {
	    	taskMasterList = taskManager.getAll();
	        return SUCCESS;
	    }
	}
	
	public List<TaskMaster> getTaskMasterList() {
		return taskMasterList;
	}

	public void setTaskMasterList(List<TaskMaster> taskMasterList) {
		this.taskMasterList = taskMasterList;
	}

	public TaskMaster getTask() {
		return task;
	}

	public void setTask(TaskMaster task) {
		this.task = task;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

}
